//
//  KXTypeSelectBtn.swift
//  KXBook
//
//  Created by  on 2022/9/10.
//

import UIKit

class KXTypeSelectBtn: UIView {
    var payBtn=UIButton()
    var inBtn = UIButton()
    var block:((_ type:KXAddType)->Void)?
    //    required init?(coder: NSCoder) {
    //        fatalError("init(coder:) has not been implemented")
    //    }
 
    override func awakeFromNib() {
        super.awakeFromNib()
        self.initUI()
    }
    func initUI(){
        self.cornerRadius = frame.height / 2
        self.borderColor = "#E27BC1".color()
        self.borderWidth = 1
        self.payBtn.addTarget(self, action:#selector(actionForBtn), for: .touchUpInside)
        self.inBtn.addTarget(self, action:#selector(actionForBtn), for: .touchUpInside)
        self.payBtn.setTitle(LS("支出"), for: .normal)
        self.inBtn.setTitle(LS("收入"), for: .normal)
        self.setupBtn(self.payBtn)
        self.setupBtn(self.inBtn)
        self.addSubview(self.payBtn)
        self.addSubview(self.inBtn)
//        self.type = self.type == .pay ? .pay : .In
        self.type = .pay
        let top = NSLayoutConstraint(item:self.payBtn, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
        let left = NSLayoutConstraint(item:self.payBtn, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1, constant: 0)
        let bottom = NSLayoutConstraint(item:self.payBtn, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
        let right = NSLayoutConstraint(item:self.payBtn, attribute: .right, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        
        let top2 = NSLayoutConstraint(item:self.inBtn, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0)
        let left2 = NSLayoutConstraint(item:self.inBtn, attribute: .left, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        let bottom2 = NSLayoutConstraint(item:self.inBtn, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0)
        let right2 = NSLayoutConstraint(item:self.inBtn, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1, constant: 0)
        self.addConstraints([top,left,bottom,right])
        self.addConstraints([top2,left2,bottom2, right2])
    }
    
    func setupBtn(_ btn:UIButton){
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.textHexColor = "#E27BC1"
        btn.textSelectedColor = "#FFFFFF"
        btn.titleLabel?.font = font(12)
    }
    var type:KXAddType = .pay
    {
        didSet{
            self.actionForBtn(type == .pay ? self.payBtn : self.inBtn)
        }
    }
    @objc func actionForBtn(_ sender:UIButton){
        sender.isSelected = true
        let ispay = sender == self.payBtn
        self.payBtn.isSelected = ispay
        self.inBtn.isSelected = !ispay
        self.payBtn.bgHexColor = ispay ? "#E27BC1" : "#FFFFFF"
        self.inBtn.bgHexColor = ispay ? "#FFFFFF" : "#E27BC1"
        self.block?(ispay ? .pay : .In)
    }
}
